//
//  ResumeController.m
//  Ten
//
//  Created by anfuer on 2018/8/21.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "ResumeController.h"
#import "UIViewController+XHPhoto.h"
#import "nilCell.h"
#import "WorkListController.h"
#import "WorkCell.h"
#import "NewResumeController.h"
#import "AddyixiangController.h"
#import "UIButton+WebCache.h"
@interface ResumeController ()
{
    NSDictionary*resume_info;
    NSString*resume_id;
    NSArray*work_experience;
    NSArray*educationals;
    NSDictionary*job_intension;
    NSArray*projects;
}
@property (weak, nonatomic) IBOutlet WxgButton *faceBtno;
@property (weak, nonatomic) IBOutlet UIButton *SaveOrEidetBtno;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *BaesLbs;
@property (weak, nonatomic) IBOutlet UITableView *workTbv;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *WorkH;
@property (weak, nonatomic) IBOutlet UITableView *jiaoyuTbv;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *JiaoyuH;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *yixiangLbs;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrH;
@property (weak, nonatomic) IBOutlet UIButton *cloeseOpenbtno;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *hidenViewH;
@property (weak, nonatomic) IBOutlet UIView *hidenview;
@property (weak, nonatomic) IBOutlet UITableView *xiangmuTbv;
@property (weak, nonatomic) IBOutlet UITextView *tsfview;
@property (weak, nonatomic) IBOutlet UITextView *tsfview2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tsfviewH2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xiangmutbvH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tsview1H;


@end

@implementation ResumeController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:NO];
    [self loaddata];
    
}

-(void)loaddata
{
    [FuncPublic getData:self parameters:nil RequestUrl:smy_resume success:^(NSDictionary *acountInfo) {
        resume_info=acountInfo[@"resume_info"];
        if ([[resume_info allKeys]containsObject:@"headimg"]&&![WXGStr(resume_info[@"headimg"])empty]) {
            [_faceBtno sd_setImageWithURL:[resume_info[@"headimg"]formatUrl] forState:UIControlStateNormal];
        }else
        {
            [_faceBtno setImage:WXGImage(@"touxiang.jpg") forState:UIControlStateNormal];
        }
        resume_id=WXGStr(acountInfo[@"resume_info"][@"resume_id"]);
        work_experience=acountInfo[@"resume_info"][@"work_experience"];
        [_workTbv reloadData];
        educationals=acountInfo[@"resume_info"][@"educationals"];
        job_intension=acountInfo[@"resume_info"][@"job_intension"];
        projects=acountInfo[@"resume_info"][@"projects"];
        _tsfview.text=acountInfo[@"resume_info"][@"evaluate"];
         _tsfview2.text=acountInfo[@"resume_info"][@"self_description"];
        _tsview1H.constant=[MyUtils GetHeightFromString:_tsfview.text Font:[UIFont systemFontOfSize:14] Width:DEVW]+70;
        _tsfviewH2.constant=[MyUtils GetHeightFromString:_tsfview2.text Font:[UIFont systemFontOfSize:14] Width:DEVW]+70;
        [_jiaoyuTbv reloadData];
        [_xiangmuTbv reloadData];
        [self initResumeinfo];
        [self initYixiangMian];
        [self ReloadScroch];
    }];
    
    
    
}

- (IBAction)hidenBja:(UIButton *)sender {
    if (sender.tag==10) {
        _tsfview.userInteractionEnabled=YES;
        [_tsfview becomeFirstResponder];
    }else
    {
        _tsfview2.userInteractionEnabled=YES;
         [_tsfview2 becomeFirstResponder];
    }

}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text empty]) {
        return;
    }
    
    NSMutableDictionary*mdic=[NSMutableDictionary dictionaryWithDictionary:@{@"resume_id":resume_id}];
    if (textView==_tsfview) {
        [mdic setObject:textView.text forKey:@"evaluate"];
    }else
    {
        [mdic setObject:textView.text forKey:@"self_description"];
    }
    [FuncPublic getData:self parameters:mdic RequestUrl:textView==_tsfview?edit_evaluate:edit_self_description success:^(NSDictionary *acountInfo) {
      
         [WSProgressHUD showImage:nil status:acountInfo[@"msg"]];
         [self loaddata];
        _tsfview.userInteractionEnabled=NO;
        _tsfview2.userInteractionEnabled=NO;
    }];

}


//tableview 加载完成可以调用的方法--因为tableview的cell高度不定，所以在加载完成以后重新计算高度

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        
        dispatch_async(dispatch_get_main_queue(), ^{
            tableView.frame = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,tableView.contentSize.width, tableView.contentSize.height);
            if (tableView==_workTbv) {
                  _WorkH.constant= tableView.contentSize.height;
            }else if (tableView==_jiaoyuTbv)
            {
                _JiaoyuH.constant= tableView.contentSize.height;
            }else if (tableView==_xiangmuTbv)
            {
                _xiangmutbvH.constant= tableView.contentSize.height;
            }
          
        });
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
     _workTbv.separatorColor=[UIColor clearColor];
     _jiaoyuTbv.separatorColor=[UIColor clearColor];
    _xiangmuTbv.separatorColor=[UIColor clearColor];
    _cloeseOpenbtno.selected=NO;
}

- (IBAction)ClosesAndOpen:(UIButton *)sender {
    sender.selected=!sender.selected;
    [self ReloadScroch];
}

//重新计算全局高度
-(void)ReloadScroch
{
    if (!_cloeseOpenbtno.selected) {
        //收起
        _hidenViewH.constant=60;
        _hidenview.hidden=YES;
          _scrH.constant=1400+_JiaoyuH.constant+_WorkH.constant;
    }
  else
  {
       //展开
      _hidenViewH.constant=_xiangmutbvH.constant+200+_tsview1H.constant+_tsfviewH2.constant;
      _hidenview.hidden=NO;
     _scrH.constant=1400+_JiaoyuH.constant+_WorkH.constant+_hidenViewH.constant;
  }

}







//初始化个人意向
-(void)initYixiangMian
{
    if (job_intension) {
        [_yixiangLbs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UILabel*lb=obj;
            NSString*str=idx==0?WXGStr(job_intension[@"type"]):idx==1?WXGStr(job_intension[@"position"]):idx==2?@"6k":idx==3?WXGStr(job_intension[@"city"]) :[NSString stringWithFormat:@"【%@】%@",job_intension[@"job_status"],job_intension[@"post_time"]];
            lb.text=str;
        }];
 
    }

}

//初始化个人基本信息
-(void)initResumeinfo
{
   
    [_BaesLbs enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel*lb=obj;
        if (idx==0) {
            lb.text=[NSString stringWithFormat:@"姓  名:%@",resume_info[@"name"]];
        }else if (idx==1)
        {
            lb.text=[NSString stringWithFormat:@"姓  别:%@",[WXGStr(resume_info[@"sex"]) isEqualToString:@"1"]?@"男":@"女"];
        }else if (idx==2)
        {
            lb.text=[NSString stringWithFormat:@"出生年月:%@",resume_info[@"birthday"]];
        }else if (idx==3)
        {
            NSArray*tempary=@[@"其他",@"初中",@"高中",@"中专",@"大专",@"本科",@"硕士",@"博士",@"博士后"];
            lb.text=[NSString stringWithFormat:@"学  历:%@",tempary[[ resume_info[@"education"]integerValue]]];
        }else if (idx==4)
        {
              NSArray*tempary2=@[@"一年",@"两年",@"三年",@"五年",@"五年以上"];
             lb.text=[NSString stringWithFormat:@"工作年限:%@",tempary2[[ resume_info[@"work_year"]integerValue]-1]];
        }else if (idx==5)
        {
            lb.text=[NSString stringWithFormat:@"手机号码:%@",resume_info[@"phone"]];
        }else if (idx==6)
        {
            lb.text=[NSString stringWithFormat:@"邮  箱:%@",resume_info[@"email"]];
        }else if (idx==7)
        {
            lb.text=[NSString stringWithFormat:@"所在城市:%@",resume_info[@"city"]];
        }
        
    }];
    
}


- (IBAction)faceBtna:(UIButton *)sender {
    [self showCanEdit:YES photo:^(UIImage *photo) {
        NSData *imageData = UIImageJPEGRepresentation(photo, 0.5);;
        [FuncPublic  UpdataONEforimgdata:imageData FileName:@"headimg" Dict:nil Url:Xedit_avatar success:^(NSDictionary *acountInfo) {
            
            [sender setImage:photo forState:UIControlStateNormal];
            
            [WSProgressHUD showImage:nil status:@"上传成功"];
        }];
    }];
    
}
- (IBAction)SaveEidetBaseBtna:(UIButton *)sender {
//    sadd_resume
       [MyUtils collviewSelidex:@"编辑基本信息" toVc:self andShopInfo:resume_info];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==_workTbv) {
        return work_experience.count>0? work_experience.count:1;
    }else if (tableView==_jiaoyuTbv)
    {
        return educationals.count>0? educationals.count:1;
    }else if (tableView==_xiangmuTbv)
    {
        return projects.count>0? projects.count:1;
    }
    
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    
    if (tableView==_workTbv) {
        if (work_experience.count>0) {
            WorkCell*cell=[WorkCell CellWithTableView:tableView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell Cellinit:work_experience[indexPath.row] Andresume_id:resume_id];
             cell.hidenLine1.hidden=NO;
             cell.hidenLine2.hidden=NO;
            if (indexPath.row==0) {
                cell.hidenLine2.hidden=YES;
                cell.hidenBtno.hidden=NO;
                cell.completion = ^(NSString * _Nonnull timechange) {
                    [self loadWorkList:work_experience Andtype:@"work"];
                };
                
            }else if (work_experience.count-1==indexPath.row)
            {
                    cell.hidenLine1.hidden=YES;
            }
            
            return cell;
        }else
        {
            nilCell*cell=[nilCell CellWithTableView:tableView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.lb1.text=@"添加工作经历";
            cell.lb2.text=@"可提交多个工作经历";
            return cell;
        }
    }else if (tableView==_jiaoyuTbv)
    {
        
        if (educationals.count>0) {
            WorkCell*cell=[WorkCell CellWithTableView:tableView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell Cellinit:educationals[indexPath.row] Andresume_id:resume_id];
            cell.hidenLine1.hidden=NO;
            cell.hidenLine2.hidden=NO;
            if (indexPath.row==0) {
                cell.hidenLine2.hidden=YES;
                cell.hidenBtno.hidden=NO;
                cell.completion = ^(NSString * _Nonnull timechange) {
                    [self loadWorkList:educationals Andtype:@"jiaoyu"];
                };
                
            }else if (work_experience.count-1==indexPath.row)
            {
                cell.hidenLine1.hidden=YES;
            }
            
            return cell;
        }else
        {
            
        nilCell*cell=[nilCell CellWithTableView:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        }
    }else if (tableView==_xiangmuTbv)
    {
        
        if (projects.count>0) {
            WorkCell*cell=[WorkCell CellWithTableView:tableView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell Cellinit:projects[indexPath.row] Andresume_id:resume_id];
            cell.hidenLine1.hidden=NO;
            cell.hidenLine2.hidden=NO;
            if (indexPath.row==0) {
                cell.hidenLine2.hidden=YES;
                cell.hidenBtno.hidden=NO;
                cell.completion = ^(NSString * _Nonnull timechange) {
                    [self loadWorkList:projects Andtype:@"xiangmu"];
                };
                
            }else if (work_experience.count-1==indexPath.row)
            {
                cell.hidenLine1.hidden=YES;
            }
            
            return cell;
        }else
        {
            
            nilCell*cell=[nilCell CellWithTableView:tableView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.lb1.text=@"添加项目经历";
            cell.lb2.text=@"可提交多个项目经历";
            return cell;
        }
       
    }
    
    return nil;
}


#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     if (tableView==_workTbv) {
         return  work_experience.count>0?150:80;
     }else if (tableView==_jiaoyuTbv)
     {
          return  educationals.count>0?120:80;
     }else if (tableView==_xiangmuTbv)
     {
          return  projects.count>0?120:80;
     }
    return 0;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (tableView==_workTbv&&work_experience.count==0) {
        [self loadWorkList:nil Andtype:@"work"];
        
    }else if (tableView==_jiaoyuTbv&&educationals.count==0)
    {
         [self loadWorkList:nil Andtype:@"jiaoyu"];
    }else if (tableView==_xiangmuTbv)
    {
         [self loadWorkList:nil Andtype:@"xiangmu"];
    }
}

-(void)loadWorkList:(NSArray*)ary Andtype:(NSString*)type
{
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"jl" bundle:nil];
    WorkListController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"WorkList"];
    secondViewController.resume_id=resume_id;
    secondViewController.tbvdata=[NSMutableArray arrayWithArray:ary] ;
    secondViewController.type=type;
    [self.navigationController pushViewController:secondViewController animated:YES];
    
}

//编辑意向
- (IBAction)EidatyixiangBtna:(id)sender {
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"jl" bundle:nil];
    AddyixiangController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Addyixiang"];
    secondViewController.resume_id=resume_id;
    if (job_intension) {
        secondViewController.passdic=job_intension;
    }
   
    [self.navigationController pushViewController:secondViewController animated:YES];
}





@end
